#!/usr/local/bin/pythonw
# -*- coding: utf-8 -*-
import re
import urllib
import time
import hashlib
import random
import cookielib, urllib2
import sys
import codecs
import os

import glob as glob
from BeautifulSoup import BeautifulSoup,BeautifulStoneSoup,NavigableString,Tag

def split_sentence(s,i) :
  motif = '(.+?(\. ))'
  l = split_generique(s,i,motif)
  return l

def split_virgulot(s,i) :
  motif = '(.+?([,;] ))'
  l = split_generique(s,i,motif)
  return l

def split_word(s,i) :
  motif = '(.+?(\s))'
  l = split_generique(s,i,motif)
  return l

def get_forest(bs) :
#  corps = bs.doc.article.corps
  corps = bs.body
  res = []
  for p in corps :
    if type(p) == Tag :
        u = []
#      try :
        s = p.string
        #seg_list = [split_word, split_virgulot, split_sentence]
        seg_list = [split_word,split_virgulot,split_sentence]
        seg_list.reverse()
        i_start= (0,len(s))
        u = rec_split(s, i_start, seg_list, 0)
#        parcourt_aux(s,u,0)
        hr = get_singleton_height_root(u)
        ms = get_max_singleton(u)
        ams =  get_all_max_singleton(u)
        for max_singleton in ams :
          print max_singleton
          su = get_subtree(u,max_singleton[1])
          print str_indented_tree(s,su)
#        print str_indented_tree(s,u)
#        print u
#        print hr,ms,ams

#        if h > 0 :
#          print s
#      except :
#        pass


def get_subtree(a, path) :
  for i in path :
    a = a[1][i]
  return a


def str_indented_tree(s,a) :
  return str_indented_tree_aux(s,a,0)


def str_indented_tree_aux(s,a,p) :
  i,sons = a
  out = "%s%s%s"%("   "*p,s[i[0]:i[1]],"\n")
  for a_sons in sons :
     out += str_indented_tree_aux(s,a_sons,p+1)
  return out

def get_height(arbre):#hauteur = hauteur fils+1
  if len(arbre[1]) == 0 :
    return 1
  return 1 + max([get_height(fils) for fils in arbre[1]])

def get_singleton_height_root(arbre):
  if len(arbre[1])!=1:
    return 0
  else:
    return 1+get_singleton_height_root(arbre[1][0])

def locate_all_singleton(arbre):
  return locate_all_singleton_aux(arbre,[],[])

def locate_all_singleton_aux(arbre,l,pos_arbre):
  h = get_singleton_height_root(arbre)
  if h == 0 :
    for i,son in enumerate(arbre[1]):
      pos_son = pos_arbre+[i]
      l = locate_all_singleton_aux(son,l,pos_son)
    return l
#  if pos_arbre == [] :
#    pos_arbre = [0]
  l.append((h,pos_arbre[:]))
  next_tree = arbre
  for i in xrange(0,h):
    next_tree = next_tree[1][0]
    pos_arbre.append(0)
  return locate_all_singleton_aux(next_tree,l,pos_arbre)

def get_max_singleton(arbre):
  l = locate_all_singleton(arbre)
  l.append((0,[0]))
  h_max = max(l)
  return h_max[0]

def get_all_max_singleton(tree):
  l = locate_all_singleton(tree)
  maxi =-1
  liste_max = []
  for h,coord in l:
    if h>maxi:
      maxi=h
      liste_max = [(h,coord)]
    elif h==maxi:
      liste_max.append((h,coord))
  return liste_max

def split_generique(chaine,inter,motif):
  s,e = inter
  length = e - s
  patt = re.finditer(motif,chaine[s:e],re.UNICODE)
  l = []
  end = s
  for match in patt:
    start = match.start() + s
    end = match.end() + s
    couple = (start,end)
    l.append(couple)
  l.append((end,e))
  return l

def rec_split(s, i, list_seg, segmenter_i):
  start,end = i
  if segmenter_i == len(list_seg) :
    return [i, []]
  segmenter = list_seg[segmenter_i]
  l = segmenter(s, i)
  res = [i, []]
  for inter in l :
    sl = rec_split(s, inter, list_seg, segmenter_i+1)
    res[1].append(sl)
  return res

def parcours_prof2list(arbre, p, l):
  l.append(arbre)
  for fils in arbre[1]:
    parcours_prof2list(fils,p+1, l)



path = sys.argv[1]
list_file = [path]




#p = td.opt_parser_deft()
#(o, _) = p.parse_args(sys.argv[1:])

#list_file = []
#if o.filein != "" :
#  list_file = [o.filein]

#else :
#  d = os.path.join(o.dirin,'*')
#  list_file = glob.glob(d)

#list_file.sort()

for path in list_file :
  content = open(path).read()
  bs = BeautifulSoup(content)
  f = get_forest(bs)
#  nb_para,l,nb_resume = count_p(bs)
#  print path, nb_para, l, nb_resume

